package model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Search for the information about every contacts in the text file and create and array of Contacts object
 * @author Marie Morin
 *
 */
public class ContactListModel {
	
	ArrayList<Contact> contactsList = new ArrayList<Contact>();
	ArrayList<Contact> newContactsList = new ArrayList<Contact>();
	
	public ContactListModel(){
		
		
		try{
			

 	        BufferedReader in = new BufferedReader(new FileReader("test.txt"));
 	        
 	        String read_phone ="1";
 	        String read_name="1";
 	        String read_fname="";
 	        String read_professionalPhone="";
 	        String read_homeAddress="";
 	        String read_professionalAddress="";
 	        String read_email="";
 	        String read_group="";
 	        String read_website="";
 	        String aname="";
 	        String read_date="";
 	        
 	        	
 	         do{

 	   	    	 read_name = in.readLine();	   	     
 	   	    	
 	   	        if (read_name != null)
 	   	        {
 	   	        	read_fname = in.readLine();
 	   	        	read_phone = in.readLine();
 	   	 	        read_email=in.readLine();
	   	 	        read_website=in.readLine();
 	   	 	        read_homeAddress=in.readLine();
 	   	        	read_professionalPhone=in.readLine();
 	   	 	        read_professionalAddress=in.readLine();
 	   	 	        read_group=in.readLine();
 	   	 	        read_date=in.readLine();
 
 	   	        	
 	   	        	aname =read_name.toUpperCase() + " "+ read_fname;
 	   	        	contactsList.add(new Contact(read_name.toUpperCase(), read_fname,aname, read_phone, read_professionalPhone, read_homeAddress, read_professionalAddress, read_email, read_group, read_website, read_date));
 	   	        } 	   	        

 	  	        }while (read_name != null);
 	          


 	        in.close();
 	        
 	        if (contactsList.size()>0){
 	        	
 	        	 this.orderContacts();
 	        }

 	        this.contactsList=this.newContactsList;
 	        
 	        
 	    }catch(IOException a){
 	        System.out.println("There was a problem:" + a);
 	    }
 	
		
	}

	public ArrayList<Contact> getContactsList() {
		return contactsList;
	}

	public void setContactsList(ArrayList<Contact> contactsList) {
		this.contactsList = contactsList;
	}

	
	/**
	 * Order the contacts in alphabetic order of their name + first name and replace the array 
	 * of contacts by the new one sorted
	 */
	public void orderContacts(){

    	Contact b = this.contactsList.get(0);
    	int size = contactsList.size();
    	int s = contactsList.size();
    	int a =0;
    	
    	for (int i=0; i<s-1; i++){
    		
    		b = contactsList.get(0);
    		a=0;
    	
    		for (int j=1; j<size; j++){
    			
    		
    			if (b.getAname().compareTo(contactsList.get(j).getAname())>0)
    			{
    				b = contactsList.get(j);
    				a=j;
    			}
    		
    		}

    		
    		newContactsList.add(b);
    		contactsList.remove(a);
    		size--;

    	
    	}
    	
    	newContactsList.add(contactsList.get(0));
    	
	}


}
